package com.eatrip.presentation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.eatrip.bean.Ricetta;
import com.eatrip.presentation.base.BaseTagView;

public class RicettaView extends BaseTagView {

	private static final long serialVersionUID = 357855106538444187L;
	private Object ricette=null;
	private String pathFoto = "";
	private int numeroElementi=0;
	private int widthImage=100;
	private int heightImage=100;
	private boolean noImage=false;
	private String basePathImage="../";
	
	
	public int doStartTag() throws JspException {
		try {
            JspWriter out = super.pageContext.getOut();
            if (ricette == null){
            	out.print("<label class=\"no-ricette\">Non ci sono ricette pubblicate dal gestore del locale</label>");
            }
            else{
            	if (!this.noImage)
            		out=this.setContenuto(out);
            	else
            		out=this.setContenutoNoImage(out);
            }
		}catch (Exception e) {
			throw new JspException("Error: " + e.getMessage());
		}
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		try {
			//JspWriter out = super.pageContext.getOut();
		} catch (Exception e) {
			throw new JspException(e.toString());
		}
		return EVAL_PAGE;
	}

	public Object getRicette() {
		return ricette;
	}

	public void setRicette(Object ricette) {
		this.ricette = ricette;
	}

	public String getPathFoto() {
		return pathFoto;
	}

	public void setPathFoto(String pathFoto) {
		this.pathFoto = pathFoto;
	}

	public int getNumeroElementi() {
		return numeroElementi;
	}

	public void setNumeroElementi(int numeroElementi) {
		this.numeroElementi = numeroElementi;
	}	
	
	public int getWidthImage() {
		return widthImage;
	}

	public void setWidthImage(int widthImage) {
		this.widthImage = widthImage;
	}

	public int getHeightImage() {
		return heightImage;
	}

	public void setHeightImage(int heightImage) {
		this.heightImage = heightImage;
	}
	
	public boolean isNoImage() {
		return noImage;
	}

	public void setNoImage(boolean noImage) {
		this.noImage = noImage;
	}

	public String getBasePathImage() {
		return basePathImage;
	}

	public void setBasePathImage(String basePathImage) {
		this.basePathImage = basePathImage;
	}

	public int getNumeroElementiInLista(){
		if (this.ricette instanceof List<?>){
			List<?> tmp = (List<?>) this.ricette;
			return tmp.size();
		}
		else if (this.ricette instanceof Collection<?>){
			Collection<?> tmp = (Collection<?>) this.ricette;
			return tmp.size();
		}
		else if (this.ricette instanceof ArrayList<?>){
			ArrayList<?> tmp = (ArrayList<?>) this.ricette;
			return tmp.size();
		}
		else if (this.ricette instanceof HashMap<?,?>){
			HashMap<?,?> tmp = (HashMap<?,?>) this.ricette;
			return tmp.size();
		}
		else if (this.ricette instanceof Ricetta){
			return 1;
		}
		return 0;
	}
	
	public JspWriter setContenuto(JspWriter out) throws IOException{
		int numeroElementiInLista = this.getNumeroElementiInLista();
		
		if (numeroElementiInLista > 1){
			
		}
		else if (numeroElementiInLista > 0){
			out.print("\n" +
						  "     <label class=\"nome-ricetta\">"+ ((Ricetta) this.ricette).getNome() +"</label>" +
						  "     <img alt=\""+ ((Ricetta) this.ricette).getNome() +"\" src=\""+ this.basePathImage + ((Ricetta) this.ricette).getPathFoto() +"\" width=\""+ widthImage +"\" height=\""+ heightImage +"\" />\n" +
						  "		<label class=\"desc-ricetta\">"+((Ricetta) this.ricette).getDescrizione() +"</label>\n" +
						  "\n");
			return out;
		}
		
		return out;
	}
	
	public JspWriter setContenutoNoImage(JspWriter out) throws IOException{
		out.print("\n" +
				  "     <label class=\"nome-ricetta\">"+ ((Ricetta) this.ricette).getNome() +"</label>" +
				  "		<label class=\"desc-ricetta\">"+((Ricetta) this.ricette).getDescrizione() +"</label>\n" +
				  "");
		return out;
	}
	
	
	
	
	
}
